<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>设置密码</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            width: 100%;
            height: 100%;
        }

        .container {
            width: 100%;
            height: 100vh;
            background-color: #f0f8ff;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .form-container {
            width: 900px;
            height: 550px;
            background: #ffffff;
            border-radius: 4px;
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .reset-form {
            width: 640px;
            height: 100%;
            display: flex;
            flex-flow: column nowrap;
            align-items: center;
            padding: 50px 30px;
        }

        .reset-form h1 {
            margin-bottom: 35px;
            color: #87cefa;
        }

        .form-section {
            width: 100%;
            margin-bottom: 30px;
            display: flex;
            align-items: flex-end;
            justify-content: center;
            gap: 6px;
        }

        .form-section label {
            font-size: 14px;
            color: #909399;
            text-transform: uppercase;
        }

        .form-section input {
            width: 50%;
            outline: 0;
            border: none;
            font-size: 18px;
            color: #008080;
            text-align: center;
            padding: 4px 10px;
            border-bottom: 1px solid rgba(0, 0, 0, 0.4);
        }

        button {
            width: 50%;
            padding: 6px 0;
            text-align: center;
            border: 3px solid #87cefa;
            border-radius: 20px;
            background: #87cefa;
            color: #fff;
            font-size: 17px;
            letter-spacing: 6px;
            text-indent: 6px;
            margin-bottom: 15px;
            cursor: pointer;
        }

        .password-rules {
            width: 50%;
            margin-bottom: 20px;
            color: #666;
            font-size: 14px;
        }
        
        .password-rules ul {
            list-style-type: none;
            padding-left: 20px;
        }
        
        .password-rules li {
            margin: 5px 0;
            color: #87cefa;
        }
        
        input::placeholder {
            color: #ccc;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="form-container">
            <div class="reset-form">
                <h1>设置密码</h1>
                <section class="form-section">
                    <label for="password">新密码</label>
                    <input type="password" id="password" placeholder="必须包含字母和数字" />
                </section>
                <section class="form-section">
                    <label for="confirmPassword">确认密码</label>
                    <input type="password" id="confirmPassword" />
                </section>
                <div class="password-rules">
                    <p>密码规则：</p>
                    <ul>
                        <li>必须包含字母和数字</li>
                        <li>长度至少8位</li>
                    </ul>
                </div>
                <button onclick="setPassword()">确认设置</button>
            </div>
        </div>
    </div>

    <script>
        function validatePassword(password) {
            // 检查是否同时包含字母和数字，且长度至少为10位
            const hasLetter = /[a-zA-Z]/.test(password);
            const hasNumber = /[0-9]/.test(password);
            const isLongEnough = password.length >= 10;
            
            if (!isLongEnough) {
                return "密码长度必须至少10位";
            }
            if (!hasLetter || !hasNumber) {
                return "密码必须同时包含字母和数字";
            }
            return null; // 验证通过
        }

        function setPassword() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (!password || !confirmPassword) {
                alert('请填写所有字段');
                return;
            }

            // 密码格式验证
            const validationError = validatePassword(password);
            if (validationError) {
                alert(validationError);
                return;
            }

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            fetch('setPassword', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: `password=${password}`
            }).then(response => response.json())
              .then(data => {
                  console.log('设置密码结果：', data);
                  alert(data.message);
                  if (data.success) {
                      window.location.href = 'game-home.html';
                  }
              }).catch(error => {
                  console.error('设置密码失败：', error);
                  alert("请求失败，请检查网络连接");
              });
        }
    </script>
</body>
</html> 